package com.shi.yibanjia.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shi.yibanjia.annotation.Auth;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.enums.AuthCodeEnum;
import com.shi.yibanjia.service.CommentsService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/admin/comment")
public class CommentController {

    @Resource
    private CommentsService commentsService;

    @Auth(hasPerm = AuthCodeEnum.BIZ_COMMENT_DQL)
    @GetMapping("/{page}/{size}")
    @Cacheable(value = "yibanjia:comment", key = "'page:'+#page+':size:'+#size+':stars:'+#stars")
    public Result list(@PathVariable("page") Integer page,
                       @PathVariable("size") Integer size,
                       @RequestParam("stars") Integer stars) {
        return Result.ok(commentsService.getCommentWithNicknameByPage(new Page<>(page, size), stars));
    }

    @Auth(hasPerm = AuthCodeEnum.BIZ_COMMENT_DQL)
    @GetMapping("/starLevel")
    @Cacheable(value = "yibanjia:comment", key = "'starsLevel'")
    public Result starLevel() {
        Integer bad = commentsService.getBadLevelCount();
        Integer medium = commentsService.getMediumLevelCount();
        Integer good = commentsService.getGoodLevelCount();
        return Result.ok(Map.of("bad", bad, "medium", medium, "good", good));
    }
}
